﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MarsExplorerServer.Models
{
    public class Game
    {
        const int MaxNumberTeams = 5;
        const int MaxNumberPlayersTeam = 5;
        public List<Team> Teams { get; set; }
        public List<Player> GlobalPlayers { get; set; }
        public List<CharType> GlobalPlayerTypes { get; set; }
        public List<Resource> GlobalResources { get; set; }
        public Game()
        {
            CharType Pilot = new CharType { Name = "Pilot" };
            CharType Comercial = new CharType { Name = "Comercial" };
            CharType Health = new CharType { Name = "Health" };
            CharType ItCom = new CharType { Name = "ItCom" };
            CharType Mech = new CharType { Name = "Mechanic" };
            Teams = new List<Team>();
            GlobalPlayers = new List<Player>();
            GlobalPlayerTypes = new List<CharType>();
            GlobalPlayerTypes.Add(Pilot);
            GlobalPlayerTypes.Add(Comercial);
            GlobalPlayerTypes.Add(Health);
            GlobalPlayerTypes.Add(ItCom);
            GlobalPlayerTypes.Add(Mech);
            GlobalResources = new List<Resource>();
            GlobalResources.Add(new Resource { Name = "H2O", Credits = 5000, Value = 0, ResourceId = 1 });
            GlobalResources.Add(new Resource { Name = "H2", Credits = 5000, Value = 0, ResourceId = 2 });
            GlobalResources.Add(new Resource { Name = "food", Credits = 20000, Value = 0, ResourceId = 3 });
            GlobalResources.Add(new Resource { Name = "O2", Credits = 5000, Value = 0, ResourceId = 4 });
            GlobalResources.Add(new Resource { Name = "Medic", Credits = 10000, Value = 0, ResourceId = 5 });
            GlobalResources.Add(new Resource { Name = "Prediction", Credits = 30000, Value = 0, ResourceId = 6 });
            GlobalResources.Add(new Resource { Name = "Shield", Credits = 5000, Value = 0, ResourceId = 7 });
            GlobalResources.Add(new Resource { Name = "Suite", Credits = 10000, Value = 0, ResourceId = 8 });
        }

        public int addTeam(Team newTeam)
        {
            if (Teams.Count() < MaxNumberTeams)
            {
                var t = from x in Teams where x.Name == newTeam.Name select x;
                if (t.Count() == 0)
                {
                    newTeam.TeamID = Teams.Count();
                    newTeam.Credits = 11000;
                    this.Teams.Add(newTeam);
                    return newTeam.TeamID;
                }
            }
            return 0;
        }

        public int addPlayer(Player newPlayer, Team playerTeam)
        {
            Team v = (from x in Teams where x.TeamID == playerTeam.TeamID select x).FirstOrDefault();
            if (v != null)
            {
                var t =
                    (from x in v.Players where x.PlayerType.Name == newPlayer.PlayerType.Name select x).FirstOrDefault();

                if (t == null)
                {
                    newPlayer.PlayerID = GlobalPlayers.Count();
                    GlobalPlayers.Add(newPlayer);
                    v.Players.Add(newPlayer);
                }
            }
            return 1;
        }

        public Team getTeam(int TeamID)
        {
            return Teams[TeamID];
        }

        public Player getPlayer(int PlayerID)
        {
            return GlobalPlayers[PlayerID];
        }

        public Resource getResource(int ResourceID)
        {
            return GlobalResources[ResourceID];
        }

        public int addResource(int teamID, int ResourceID, int value)
        {
            Team teamSelected = getTeam(teamID);
            Resource resourceSelected = getResource(ResourceID);
            if (teamSelected.Credits - resourceSelected.Credits > 0)
            {
                teamSelected.Credits -= resourceSelected.Credits;
                Resource t = (from x in teamSelected.Resources where x.ResourceId == ResourceID select x).FirstOrDefault();

                if (t != null)
                {
                    t.Value += value;
                }
                else
                {
                    resourceSelected.Value = value;
                    teamSelected.Resources.Add(resourceSelected);
                }
            }
            return 1;
        }
    }
}